//
//  DHAdvicesViewController.m
//  DHKuaiYi
//
//  Created by 韩旭 on 16/11/15.
//  Copyright © 2016年 30-陈辉华. All rights reserved.
//

#import "DHAdvicesViewController.h"
#import <Masonry.h>
#import "DHSetandHelpTableViewCell.h"
#import "UITextView+YLTextView.h"
#import <SVProgressHUD.h>

#define screenWidth [UIScreen mainScreen].bounds.size.width
#define delay(time) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((time) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[SVProgressHUD dismiss];})

@interface DHAdvicesViewController ()<UITableViewDataSource, UITableViewDelegate,UITextViewDelegate>

/* 背景图片 */
@property (strong, nonatomic) UIImageView *backgroundImg;

/* text */
@property (weak, nonatomic) UITextView *advicesTextView;

/* tableview */
@property (strong, nonatomic) UITableView *advicesTableView;

/* 图片数组 */
@property (strong, nonatomic) NSArray *imgArray;

/* 标题数组 */
@property (strong, nonatomic) NSArray *labelArray;

/* 提交 */
@property (strong, nonatomic) UIButton *submitButton;

@end

@implementation DHAdvicesViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    
    //背景图
    UIImageView *backgroundImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.backgroundImg=backgroundImg;
    backgroundImg.image = [UIImage imageNamed:@"pic_hd_1.png"];
    [self.view addSubview:backgroundImg];
    
    //文本
    UITextView *advicesTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 60, screenWidth, 270)];
    self.advicesTextView= advicesTextView;
    advicesTextView.placeholder = @"请输入您对我们的宝贵意见";
    advicesTextView.limitLength = [NSNumber numberWithInt:20];
    [self.view addSubview:advicesTextView];
    
    //表格
    UITableView *advicesTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    //设置数据源对象,代理
    advicesTableView.dataSource = self;
    advicesTableView.delegate = self;
    advicesTableView.bounces = NO;
    advicesTableView.backgroundColor = [UIColor XHH_colorWithHex:0x62C6C4];
    [self.view addSubview:advicesTableView];
    
    //注册cell
    [advicesTableView registerClass:[DHSetandHelpTableViewCell class] forCellReuseIdentifier:@"cell"];
    [advicesTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(advicesTextView.mas_bottom);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(118);
    }];
    
    //按钮
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom ];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:submitButton];
    self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, -10);
    [self.view addSubview:submitButton];
    
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    submitButton.backgroundColor = [UIColor clearColor];
    submitButton.frame = CGRectMake(0, 0, 80, 40);
    submitButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [submitButton addTarget:self action:@selector(BtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    //表格的数据
    self.labelArray = [NSArray arrayWithObjects:@"联系我们",@"用户交流群", nil];
    self.imgArray = [NSArray arrayWithObjects:@"medtronic_phone",@"medtronic_service", nil];
}

/**
 提交的点击事件
 */
- (void)BtnClick {
    
    if (self.advicesTextView.text.length != 0) {
        
        [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
        [SVProgressHUD show];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            delay(1.2);
            self.advicesTextView.text = @"";
            [self.navigationController popViewControllerAnimated:YES];
        });
        
    }else if(self.advicesTextView.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"不能提交空内容喔，亲"];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    }
    delay(1);
}

#pragma mark - UITableViewDataSource

/**
 行数
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

/**
 单元格
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DHSetandHelpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.label.text = self.labelArray[indexPath.item];
    cell.icon.image = [UIImage imageNamed:self.imgArray[indexPath.item]];
    return cell;
}

#pragma mark -UITableViewDelegate
/**
 返回组头高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    //设置组头颜色
    [[UITableViewHeaderFooterView appearance] setTintColor:[UIColor XHH_colorWithHex:0x62C6C4]];
    return 30;
}

/**
 返回组头文字
 */
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString* str = @"欢迎联系我们";
    return str;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end










     
   
